package com.zjuee.web.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.auth.RoleDto;
import com.zjuee.base.model.common.auth.Role;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Update;
import com.zjuee.service.auth.service.RoleService;
import com.zjuee.service.auth.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色 web 接口
 *
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(path = Common.Service.ZJUEE_AUTH_ROLE_URL_PREFIX, name = Common.Service.ZJUEE_AUTH_SERVICE_NAME)
public class RoleController {

    @Resource
    private RoleService roleService;

//    @Resource
//    private LoginDto loginDto;

    //角色创建
    @PostMapping("/add")
    public R<Long> add(@Validated(Insert.class) @RequestBody Role role) {
        try {
            Long add = roleService.add(role);
            if (null != add) {
                return R.ok(add);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //根据id获取角色详情（不级联子集）
    @GetMapping("/info/{id}")
    public R<Role> selectById(@PathVariable("id") Long id) {
        try {
            Role select = roleService.selectById(id,0L);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 分页查询 Role
     *
     * @param roleDto Dto
     * @return Page<Role>
     */
    @PostMapping("/list")
    public R<Page<Role>> list(@RequestBody RoleDto roleDto) {
        try {
            Page<Role> page = roleService.list(roleDto);
            if (null != page) {
                return R.ok(page);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    //修改角色信息,只允许修改name、code、remark字段
    @PutMapping("/update")
    public R<Boolean> update(@Validated(Update.class) @RequestBody Role role) {
        try {
            Boolean update = roleService.update(role);
            if (update) {
                return R.ok(update);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //根据id获取角色列表（子集）-不分页
    @GetMapping("/subset/info/{id}")
    public R<List<Role>> selectByParentId(@PathVariable("id") Long id) {
        try {
//            LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<Role> select = roleService.selectByParentId(id,0L);

            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }



    //删除角色
    @DeleteMapping("/delete/{roleId}")
    public R<Boolean> deleteRole(@PathVariable("roleId") Long id) {
//        LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {

            return roleService.delete(id, 0L) ? R.ok() : R.fail();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取角色下的所有资源 （树形）
     *
     * @param roleId Long
     * @return MenuVo
     */
    @GetMapping("/menus/{roleId}")
    public R<List<MenuVo>> findAllMenusByRoleId(@PathVariable("roleId") Long roleId) {
        try {
            List<MenuVo> menuVoList = roleService.findAllMenu(roleId);
            if (null != menuVoList) {
                return R.ok(menuVoList);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }
}
